<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: 奇辰科技
 * @Date: 2021-09-06 00:28:35
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-15 22:23:59
 */
namespace App\Http\Controllers\Scrm\Fx;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Scrm\Fx\RuleModel;
use Illuminate\Http\Request;

class RuleController extends Controller
{
	public function get(Request $request) {
		$level = RuleModel::where('id', '=', $request->input('id'))->first();
		$result = ResultTool::success();
		$result['data']['level'] = $level;
		return $result;
	}

	public function list() {
		$items = RuleModel::get();
		$result = ResultTool::success();
		$result['data']['items'] = $items;
		return $result;
	}

	public function create(Request $request) {
		$rule = new RuleModel();
		$rule->deduct_level = $request->input('deduct_level');
		$rule->level_related = $request->input('level_related');
		if($request->input('pre_level_id') != null && $request->input('pre_level_id') != '') {
			$rule->pre_level_id = $request->input('pre_level_id');
		}
		if($request->input('next_level_id') != null && $request->input('next_level_id') != '') {
			$rule->next_level_id = $request->input('next_level_id');
		}
		$rule->deduct_mode = $request->input('deduct_mode');
		$rule->deduct_money = $request->input('deduct_money');
		$rule->deduct_ratio = $request->input('deduct_ratio');
		$rule->save();
		$result = ResultTool::success();
		return $result;
	}
	
	public function update(Request $request) {
		$rule = RuleModel::where('id', '=', $request->input('id'))->first();
		$rule->deduct_level = $request->input('deduct_level');
		$rule->level_related = $request->input('level_related');
		if($request->input('pre_level_id') != null && $request->input('pre_level_id') != '') {
			$rule->pre_level_id = $request->input('pre_level_id');
		}
		if($request->input('next_level_id') != null && $request->input('next_level_id') != '') {
			$rule->next_level_id = $request->input('next_level_id');
		}
		$rule->deduct_mode = $request->input('deduct_mode');
		$rule->deduct_money = $request->input('deduct_money');
		$rule->deduct_ratio = $request->input('deduct_ratio');
		$rule->save();
		$result = ResultTool::success();
		return $result;
	}

	public function delete(Request $request) {
		RuleModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}
}